package com.rosense.server.config;

import java.util.HashMap;
import java.util.Map;

import org.dom4j.Element;

import com.rosense.client.i.IWriteReader;
import com.rosense.client.rpc.RPCBean;
import com.rosense.commons.LangUtils;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月23日 下午3:59:47
 * 
 */
public class HostBean {
	public String name;
	public Map<String, RPCBean> rpcMap = new HashMap<String, RPCBean>();
	public boolean timeout = true;
	public long time = 0;

	public HostBean(String name) {
		this.name = name;
	}

	public HostBean(Element ele) {
		refresh(ele);
	}

	public void refresh(Element ele) {
		this.name = ele.attributeValue("name");
	}

	public void setRpcBean(RPCBean bean) {
		IWriteReader writeReader = HostCaches.wrMap.get(bean.getName());
		rpcMap.put(bean.getName(), bean);
		time = System.currentTimeMillis();
		if (writeReader != null) {
			writeReader.write(name, bean);
		}
		checkTime();
	}

	public void checkTime() {
		int second = LangUtils.toInt(HostCaches.params.get("second"), 0);
		timeout = (System.currentTimeMillis() - time > second * 3 * 1000);
	}
}
